<template>
  <div style="border: 1px solid #ccc;z-index: 100;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      :style="{'height': height, 'width':width , 'overflow-y': 'hidden'}"
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
    />
  </div>
</template>

<script setup lang='ts'>
import { nextTick, reactive, watch } from 'vue';
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

let editor = null
const props = defineProps({
    content:{
      type:String,
      default:''
    },
    width:{
      type:String,
      default:'auto'
    },
    height:{
      type:String,
      default:'300px'
    },
})
let  toolbarConfig = reactive({})
let  editorConfig = reactive({ placeholder : '请输入内容...' })
let  mode = 'default' // or 'simple'
// 编辑器实例，必须用 shallowRef
let editorRef = shallowRef()

// 内容 HTML
let valueHtml = ref()
watch(()=> [props.content],
(val)=>{
    valueHtml.value = val[0]
  }
)

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})

const handleCreated = (editor) => {
  editorRef.value = editor // 记录 editor 实例，重要！
}
defineExpose({content:valueHtml})
</script>

<style lang='scss' scoped>

</style>
